package com.h3c.nem.controller;

import com.alibaba.fastjson.JSONObject;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.github.pagehelper.PageInfo;
import com.h3c.nem.dto.PageQueryDTO;
import com.h3c.nem.dto.TbOfferCompanyRelaDTO;
import com.h3c.nem.dto.beisen.PtEnumDTO;
import com.h3c.nem.entity.SdEmailTemplate;
import com.h3c.nem.entity.TbOfferCompanyRela;
import com.h3c.nem.service.ITbOfferCompanyRelaService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 控制层
 *
 * @author chenyifan
 * @since 2022-06-14 16:39:01
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/tbOfferCompanyRela")
public class TbOfferCompanyRelaController {
    /**
     * 服务对象
     */
    private final ITbOfferCompanyRelaService tbOfferCompanyRelaService;

    /**
     * @Description: 枚举码对应的枚举集合
     * @Param: enumCode 枚举码
     * @Author: huangweizhen
     * @Date: 2022/6/21
     */
    @ApiOperation("获取枚举码对应的枚举集合")
    @PostMapping(value = "/getEnumList")
    public Result getEnumList(@RequestParam String enumCode) {
        try {
            if (StringUtils.hasText(enumCode)) {
                List<PtEnumDTO> list = tbOfferCompanyRelaService.getEnumList(enumCode);
                return new Result(true, StatusCodeEnum.NORMAL, list);
            }
            return new Result(false, StatusCodeEnum.SEARCH_FAIl.getCode(), "枚举码不可空值传入");
        } catch (Exception ex) {
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), ex.getMessage());
        }
    }


    /**
     * @Description: 获取模板的集合
     * @Param: emailTemplateType 模板类型：1 沟通函模板；99 Offer模板
     * @Author: huangweizhen
     * @Date: 2022/6/22
     */
    @ApiOperation("获取模板的集合")
    @GetMapping("/getTemplateCodeNameList")
    public Result getTemplateCodeNameList(@RequestParam Integer emailTemplateType) {
        try {
            List<SdEmailTemplate> sdEmailTemplateList = tbOfferCompanyRelaService.getTemplateCodeNameList(emailTemplateType);
            return new Result(true, StatusCodeEnum.NORMAL, sdEmailTemplateList);
        } catch (Exception ex) {
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), ex.getMessage());
        }
    }

    /**
     * @Description: 获取人事范围和Offer模板对应关系
     * @Param: pageQuery 分页参数
     * @Author: huangweizhen
     * @Date: 2022/6/22
     */
    @ApiOperation("取人事范围和Offer模板对应关系")
    @PostMapping("/queryFuzzyListForPage")
    public Result queryFuzzyListForPage(@RequestBody PageQueryDTO pageQuery) {
        try {
            //查询分页数据
            PageInfo<TbOfferCompanyRelaDTO> fuzzyListForPage = tbOfferCompanyRelaService.getFuzzyListForPage(pageQuery);
            return new Result(true, StatusCodeEnum.NORMAL, fuzzyListForPage);
        } catch (Exception ex) {
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), ex.getMessage());
        }
    }

    /**
     * @Description: 插入或更新人事范围和Offer模板对应关系
     * @Param: info 对应关系模型数据
     * @Author: huangweizhen
     * @Date: 2022/6/23
     */
    @ApiOperation("插入或更新人事范围和Offer模板对应关系")
    @PostMapping("/saveInfo")
    public Result saveInfo(@RequestBody TbOfferCompanyRela info) {
        try {
            boolean isOK = tbOfferCompanyRelaService.saveInfo(info);
            if (isOK) {
                return new Result(true, StatusCodeEnum.NORMAL.getCode(), "保存成功");
            }
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), "保存失败，请重试");
        } catch (Exception ex) {
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), ex.getMessage());
        } finally {
            log.info("插入或更新人事范围和Offer模板对应关系:" + JSONObject.toJSONString(info));
        }
    }

    /**
     * @Description: 删除人事范围和Offer模板对应关系
     * @Param: relaId 对应关系ID
     * @Author: huangweizhen
     * @Date: 2022/6/23
     */
    @ApiOperation("删除人事范围和Offer模板对应关系")
    @PostMapping("/delInfo")
    public Result delInfo(@RequestParam String relaId) {
        try {
            boolean isOK = tbOfferCompanyRelaService.delInfo(relaId);
            if (isOK) {
                return new Result(true, StatusCodeEnum.NORMAL.getCode(), "删除成功");
            }
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), "删除失败，请重试");
        } catch (Exception ex) {
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), ex.getMessage());
        } finally {
            log.info("删除人事范围和Offer模板对应关系:" + relaId);
        }
    }
}

